<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@rg.by>	                  |
// | Copyright (c) 2004 Max Baryshnikov                                   |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 13.11.04 (dd/mm/yy)
// $Id: startup.js.php,v 1.5 2006/02/20 08:39:46 sheiko Exp $

global $http_path, $env;


$add_js = <<<EOD


var vswitcher = new Array();
var BtnDownStatus = 0;
	function displayCSS(obj_name) {
		
		var theStyle = new Array();
		var theStyleText = "";
		var newOption;
		var Str;
		
		var obj =  document.getElementById(obj_name);

		var noOfSheets = document.styleSheets.length;
		if (noOfSheets > 0) {
			for (i=1;i<=noOfSheets;i++) {
				if (document.styleSheets(i-1).href.indexOf("usr/system/") < 0) {
				theStyleText = theStyleText + document.styleSheets(i-1).cssText;
				}
			}
		}
		re = /([\s])/g;
		theStyleText = theStyleText.replace(re, "");
		re = /\{.*?\}/gm;
		theStyle = theStyleText.replace(re, "|").split("|");
		if(theStyle.length>0) {
			for (i=1;i<=theStyle.length;i++) {
			  if(theStyle[i-1].indexOf(".")==0 || theStyle[i-1].indexOf("SPAN.")==0) {
			  	if(theStyle[i-1].substr(2).indexOf(".")<0 ) {
						newOption = document.createElement("option");
						Str = theStyle[i-1].replace(".","");
						newOption.setAttribute("value", Str, "");
						text=document.createTextNode(theStyle[i-1]);
						newOption.appendChild(text);
						obj.appendChild(newOption);
			  	}
			  }
			}
		} else return false;
	return true;
	} 
function applyStyle(obj, styleValue) {
		var done = false;
		var HeaderVal;
		
		re = /^H\d$/gi;
		
		var selectedArea = obj.document.selection.createRange();
		if (obj.document.selection.type == "Control") {
			applyStyleTo = selectedArea.commonParentElement();
		}  else {
			if (obj.document.selection.createRange().htmlText == "") {
				return false;
			} else {
			

				if( selectedArea.parentElement().tagName.match(re) != null && styleValue=="") {
					selectedArea.parentElement().removeNode(false);
				}

				HeaderVal = styleValue.match(re);
				if(HeaderVal!=null) {
					if( selectedArea.parentElement().tagName.toUpperCase() == HeaderVal ) {
						selectedArea.parentElement().removeNode(false);
					} else
					selectedArea.pasteHTML("<"+HeaderVal+">" + selectedArea.htmlText + "</"+HeaderVal+">");
				}
				
				
				if ((selectedArea.parentElement().tagName.toUpperCase() == "SPAN") || (selectedArea.parentElement().tagName.toUpperCase() == "A")) {
					applyStyleTo = selectedArea.parentElement();
					if ((styleValue == "") && (selectedArea.parentElement().tagName.toUpperCase() == "SPAN")) {
						applyStyleTo.removeNode(false);
						done = true;
					}
				} else {
					if (styleValue != "") {
						selectedArea.pasteHTML("<span class=" + styleValue + ">" + selectedArea.htmlText + "</span>");
					}
					done = true;
				}
			}
		}
		if (done != true) {
			applyStyleTo.className = styleValue;
		}
		return false;
	}

	function button_up(eButton){
		eButton.style.borderBottom = "buttonshadow solid 1px";
		eButton.style.borderLeft = "buttonhighlight solid 1px";
		eButton.style.borderRight = "buttonshadow solid 1px";
		eButton.style.borderTop = "buttonhighlight solid 1px";
		eButton = null;
	}

	function button_down(eButton){
		eButton.style.borderBottom = "buttonhighlight solid 1px";
		eButton.style.borderLeft = "buttonshadow solid 1px";
		eButton.style.borderRight = "buttonhighlight solid 1px";
		eButton.style.borderTop = "buttonshadow solid 1px";
	}

	function button_over(eButton){
		if(BtnDownStatus==0) {
		eButton.style.borderBottom = "buttonshadow solid 1px";
		eButton.style.borderLeft = "buttonhighlight solid 1px";
		eButton.style.borderRight = "buttonshadow solid 1px";
		eButton.style.borderTop = "buttonhighlight solid 1px"; }
	}
	function button_out2(eButton){
		if(BtnDownStatus==0) {
		eButton.style.borderColor = "white";} 
	}
	
	function button_out(eButton){
		eButton.style.borderColor = "white";
	}
	
function menu(obj, uniqname) {
	var el = document.getElementById('panel_'+uniqname+'_but');
	if(	obj.style.display == "none" ) 	{ 
		obj.style.display = "block"; 
		el.src = "{$env["http_path"]}/usr/system/images/but_minus.gif"; 
	} else	{
		obj.style.display = "none"; 
		el.src = "{$env["http_path"]}/usr/system/images/but_plus.gif";  }
	return false;
}

	
function cleanHTML(selfobj) {
	
	//selfobj =  document.getElementById('area_downloadarea');
	obj =  selfobj.document;

	if(obj.selection.createRange().text=="") { 
	code = selfobj.innerHTML;
	} else {
	code = obj.selection.createRange().text;
	}
		code = code.replace(/<font.*?>/gi, "");
		code = code.replace(/<\/font>/gi, "");
		code = code.replace(/<span.*?>/gi, "");
		code = code.replace(/<\/span>/gi, "");
		code = code.replace(/<p([^>])*>(&nbsp;)*\s*<\/p>/gi,"");
		code = code.replace(/<span([^>])*>(&nbsp;)*\s*<\/span>/gi,"");
		code = code.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, "<$1$3");
		code = code.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
		code = code.replace(/<\\?\??xml[^>]>/gi, "");
		code = code.replace(/<\/?\w+:[^>]*>/gi, "");
		code = code.replace(/&nbsp;/gi, "");

		if(obj.selection.createRange().text=="") { 
			selfobj.innerHTML = code;
		} else 	{
			obj.selection.clear(); 
			obj.selection.createRange().pasteHTML(code);
		}
		obj.focus();
}

function DeleteFileObj(objname) {
	document.getElementById('file_'+objname).value='';
	document.getElementById('show_'+objname).innerHTML='';
	return false;
}

function DeleteImageObj(objname) {
	document.getElementById('image_'+objname).value='';
	document.getElementById('src_'+objname).src='{$env["http_path"]}/usr/system/images/x.gif';
	return false;
}



function inserthtml(obj, sHTML) { 
	if(obj.selection.type == "Control")
		obj.selection.clear();
	obj.selection.createRange().pasteHTML(sHTML);
	obj.focus();
}

function ecommand(var_area, command) {
	obj = document.getElementById("area_"+var_area);
	obj.document.selection.createRange().execCommand(command);
	obj.focus();
	BtnDownStatus = 1;
 	return false;
}


function insimage(obj, tag) {

  var args=new Array();
  var sHTML;

  if(obj.selection.type == "Control")
	{
	    var oImg = obj.selection.createRange().item(0);
	    bcwb_form.arg_ImgUrl.value = oImg.src;
	    bcwb_form.arg_AltText.value = oImg.alt;
	    bcwb_form.arg_ImgBorder.value = oImg.border;
	    bcwb_form.arg_HorSpace.value = oImg.hspace;
	    bcwb_form.arg_VerSpace.value = oImg.vspace;
	    bcwb_form.arg_ImgAlign.value = oImg.align;
	    bcwb_form.arg_ImgHeight.value = oImg.height;
	    bcwb_form.arg_ImgWidth.value = oImg.width;
	}	    
EOD;

$add_js .= '			window.open(\''.$env["http_path"].'usr/system/insert_image.php?tag=\'+tag+\'&exp=new\', \'displayWindow\',\'width=500,height=530,status=yes,toolbar=no,menubar=no, scrollbars=auto, resizable=yes\');
return false;
} ';

$add_js .=' function insfile(obj, tag) {
	window.open(\''.$env["http_path"].'usr/system/insert_file.php?tag=\'+tag, \'displayWindow\',\'width=350,height=200,status=yes,toolbar=no,menubar=no, scrollbars=auto, resizable=yes\'); 
	return false;
}

function inslink(obj, tag) {
	window.open(\''.$env["http_path"].'usr/system/insert_link.php?tag=\'+tag, \'displayWindow\',\'width=350,height=230,status=yes,toolbar=no,menubar=no, scrollbars=auto, resizable=yes\'); 
	return false;
}
function inspage(obj, tag) {
	window.open(\''.$env["http_path"].'usr/system/insert_page.php?tag=\'+tag, \'displayWindow\',\'width=350,height=230,status=yes,toolbar=no,menubar=no, scrollbars=auto, resizable=yes\'); 
	return false;
}


function getCursorPos(obj) {
	obj.focus();
	var sel=obj.document.selection.createRange();
	sel.moveStart(\'textedit\',-1);
	return sel.text.length;
}

function allEvents(var_name) {
	BtnDownStatus=0;
	selobj = document.getElementById("styles_"+var_name);
	obj = document.getElementById("area_"+var_name);
//	obj.focus();	

	var sel = obj.document.selection.createRange();
	if(obj.document.selection.type != "Control") {
		var tag = sel.parentElement().tagName;
		var thisclass = sel.parentElement().className;
	
		re = /^H\d$/gi;
		var HnTag = tag.match(re);
		
		if( tag.length>0 ) selobj.selectedIndex = 0;
		
		if( HnTag != null ) {
			for(i=0; i<selobj.length; i++) {
				if(selobj.item(i).value.toUpperCase()==HnTag) selobj.selectedIndex = i;
			}
		} else {
			if(thisclass.length>0) {
				for(i=0; i<selobj.length; i++) {
					if(selobj.item(i).value==thisclass) selobj.selectedIndex = i;
				}
			}
		}
		
		
		if(tag.toUpperCase()=="STRONG" || tag.toUpperCase()=="B") {
			button_down(document.getElementById("fontBold_"+var_name)); }
		else button_out2(document.getElementById("fontBold_"+var_name));
		if(tag.toUpperCase()=="EM" || tag.toUpperCase()=="I") {
			button_down(document.getElementById("fontItalic_"+var_name)); }
		else button_out2(document.getElementById("fontItalic_"+var_name));
		if(tag.toUpperCase()=="U") {
			button_down(document.getElementById("fontUnderline_"+var_name)); }
		else button_out2(document.getElementById("fontUnderline_"+var_name));
		if(tag.toUpperCase()=="LI") {
			button_down(document.getElementById("fontInsertOrderedList_"+var_name)); }
		else button_out2(document.getElementById("fontInsertOrderedList_"+var_name));	
		if(tag.toUpperCase()=="BLOCKQUOTE") {
			button_down(document.getElementById("indent_"+var_name)); }
		else button_out2(document.getElementById("indent_"+var_name));	
		if(tag.toUpperCase()=="SUP") {
			button_down(document.getElementById("fontSuperScript_"+var_name)); }
		else button_out2(document.getElementById("fontSuperScript_"+var_name));		
		if(tag.toUpperCase()=="SUB") {
			button_down(document.getElementById("fontSubScript_"+var_name)); }
		else button_out2(document.getElementById("fontSubScript_"+var_name));		
		
	}	
	

}

	function htmltotext(selfobj, el) { 

	obj =  selfobj.document;
	
	var args = new Array();
	var saved_html = selfobj.innerHTML;
	args["code"] = saved_html;
	args["InnerLen"] = selfobj.document.selection.createRange().htmlText.length;
	args["OldPos"] = getCursorPos(selfobj);

	var CorrectXHTML = false;
	//var CorrectXHTML = get_xhtml(obj, "'.$default_language.'", "'.$default_charset.'"); 
	//if(CorrectXHTML) args["code"] = CorrectXHTML;

// Inserting index code
	text = "{[|position|]}";
	r = selfobj.document.selection.createRange();
	c = r.duplicate();
	if (r && c) {
	c.text = c.text.charAt(c.text.length - 1) == " " ? text + " " : text;
	} else c.value = text;
	
	pos_of_code = selfobj.innerHTML.indexOf(text);
	args["CursorPos"] = pos_of_code;


	if(CorrectXHTML) {
		selfobj.innerHTML = CorrectXHTML;	
		obj.focus();
	} else 
	selfobj.innerHTML = saved_html;

	arr = null;
	arr = showModalDialog( "'.$env["http_path"].'usr/system/edit.php",
	                          args,
	                         "font-family:Verdana; font-size:12px; dialogWidth:700px; dialogHeight:600px");

	if(arr != null) selfobj.innerHTML = arr["code"]; 

//	 r.collapse(true);
//	 r.moveStart("character", args["OldPos"]-args["InnerLen"]);
//	 r.moveEnd("character", 0);
//	 r.select();

 	  return false; 
	} 
';

$add_js .= '
	function StrFind(obj) {
		showModelessDialog(\''.$env["http_path"].'usr/system/find.html\', obj, "resizable: no; help: no; status: no; scroll: no; dialogWidth:400px; dialogHeight:200px");
		return false;
	}
';

?>